package connectionstringgenerator.connectionStringGenerator;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ConnectionStringGeneratorShell extends Shell {

	private Text txtCim;
	private Text txtInstance;
	private Text txtUser;
	private Text txtPassword;
	private Label lblAdatbzisNeve;
	private Text txtDatabaseName;
	private Label lblAktulisConnectionstring;
	private Label txtCurrent;
	private Button btnGenerate;

	private String server;
	private String instance;
	private String user;
	private String pass;
	private String dbName;

	public ConnectionStringGeneratorShell(Display display) {
		super(display);
		setLayout(new GridLayout(2, false));

		Label lblServerNeves = new Label(this, SWT.NONE);
		lblServerNeves.setText("Szerver IP c\u00EDme (\u00E9s port) :");

		txtCim = new Text(this, SWT.BORDER);
		txtCim.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				server = txtCim.getText();
				txtCurrent.setText(createConnectionString());
				txtCurrent.pack();
			}
		});
		txtCim.setToolTipText("Pl. 127.0.0.1:1433");
		txtCim.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label lblSzerverInstanceNeve = new Label(this, SWT.NONE);
		lblSzerverInstanceNeve.setText("Szerver instance neve: ");

		txtInstance = new Text(this, SWT.BORDER);
		txtInstance.setToolTipText("Pl. MSSQLSERVER");
		txtInstance.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		txtInstance.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				instance = txtInstance.getText();
				txtCurrent.setText(createConnectionString());
				txtCurrent.pack();
			}
		});

		lblAdatbzisNeve = new Label(this, SWT.NONE);
		lblAdatbzisNeve.setText("Adatb\u00E1zis neve: ");

		txtDatabaseName = new Text(this, SWT.BORDER);
		txtDatabaseName.setToolTipText("Pl. AudiAkademie");
		txtDatabaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		txtDatabaseName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				dbName = txtDatabaseName.getText();
				txtCurrent.setText(createConnectionString());
				txtCurrent.pack();
			}
		});

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setText("Adatb\u00E1zis felhaszn\u00E1l\u00F3 neve: ");

		txtUser = new Text(this, SWT.BORDER);
		txtUser.setToolTipText("Pl. FoglaloUser");
		txtUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		txtUser.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				user = txtUser.getText();
				txtCurrent.setText(createConnectionString());
				txtCurrent.pack();
			}
		});

		Label lblFelhasznaloNev = new Label(this, SWT.NONE);
		lblFelhasznaloNev.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblFelhasznaloNev
				.setText("Adatb\u00E1zis felhaszn\u00E1l\u00F3 jelszava: ");

		txtPassword = new Text(this, SWT.BORDER);
		txtPassword.setToolTipText("Pl. jelszo");
		txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		txtPassword.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				pass = txtPassword.getText();
				txtCurrent.setText(createConnectionString());
				txtCurrent.pack();
			}
		});

		lblAktulisConnectionstring = new Label(this, SWT.NONE);
		lblAktulisConnectionstring.setText("Aktu\u00E1lis connectionString: ");

		txtCurrent = new Label(this, SWT.NONE);
		txtCurrent.setText(createConnectionString());

		btnGenerate = new Button(this, SWT.NONE);
		btnGenerate.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				PrintWriter writer = null;
				try {
					writer = new PrintWriter("connectionString.txt", "UTF-8");
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				writer.print(createConnectionString());
				writer.close();
			}
		});
		btnGenerate.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 2, 1));
		btnGenerate.setText("Gener\u00E1l");
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
	}

	private String createConnectionString() {
		StringBuilder sb = new StringBuilder();
		sb.append("jdbc:sqlserver://");
		sb.append(server);
		sb.append(";databasename=");
		sb.append(dbName);
		sb.append(";instance=");
		sb.append(instance);
		sb.append(";user=");
		sb.append(user);
		sb.append(";password=");
		sb.append(pass);
		return sb.toString();
	}

	public static void main(String[] args) {
		Display display = new Display();
		ConnectionStringGeneratorShell mainShell = null;
		mainShell = new ConnectionStringGeneratorShell(display);
		mainShell.setSize(750, 220);
		mainShell.open();
		mainShell.setText("Connection String Generator");

		while (!mainShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		mainShell.dispose();
	}

	@Override
	public void checkSubclass() {
		//
	}

}
